// featurePackage/pages/mySmallShop/mySmallShop.js
const app = getApp();

import {
  HOMEWODEDIAN_IMG
} from "../../../service/image_service"
import {
  queryMyShopGoodList,
  deleteMyShopGoods,
  queryOtherShopGoodList,
  queryShopGoodList
} from "../../../service/jb_smallshop_service"
import {
  loginWxAction
} from "../../../service/config"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    isShareEnter: false,//是否是分享进来的
    shareUserId: null,
    pageStatus: 0,//0:管理状态，1：就是删除状态
    homewodedian_img: HOMEWODEDIAN_IMG,
    goodList: [
      // {
      //     "id": 8,
      //     "image": "https://images.jiabao19.com/images/public/maintain/2021/12/25/7a6475bba07c47bb98e5db0e0d21c8ebq5sw2kijlc.jpg",
      //     "storeName": "兰蔻（LANCOME）口红196 全新菁纯丝绒雾面唇膏 化妆品礼盒",
      //     "price": "787.00",
      //     "otPrice": "197.00",
      //     "sales": null,
      //     "ficti": null,
      //     "unitName": null,
      //     "activity": "0,1,2,3",
      //     "activityH5": null,
      //     "cartNum": null,
      //     "stock": null,
      //     "flatPattern": null,
      //     isSelect:false,
      //   },
      //   {
      //     "id": 6,
      //     "image": "https://images.jiabao19.com/images/public/maintain/2021/12/25/fef1aee22a04466d93693f5791d44bfd2rsm469mcr.jpg",
      //     "storeName": "第一江南 西湖龙井茶叶礼盒 明前特级龙井绿茶【至臻献礼200g】礼盒装",
      //     "price": "1588.00",
      //     "otPrice": "1688.00",
      //     "sales": null,
      //     "ficti": null,
      //     "unitName": null,
      //     "activity": "0,1,2,3",
      //     "activityH5": null,
      //     "cartNum": null,
      //     "stock": null,
      //     "flatPattern": null,
      //     isSelect:false,
      //   },
      //   {
      //     "id": 6,
      //     "image": "https://images.jiabao19.com/images/public/maintain/2021/12/25/fef1aee22a04466d93693f5791d44bfd2rsm469mcr.jpg",
      //     "storeName": "第一江南 西湖龙井茶叶礼盒 明前特级龙井绿茶【至臻献礼200g】礼盒装",
      //     "price": "1588.00",
      //     "otPrice": "1688.00",
      //     "sales": null,
      //     "ficti": null,
      //     "unitName": null,
      //     "activity": "0,1,2,3",
      //     "activityH5": null,
      //     "cartNum": null,
      //     "stock": null,
      //     "flatPattern": null,
      //     isSelect:false,
      //   }
    ],
    goodType: 'leftright', //leftright就是普通展示的, select：带选择的
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.isShare == 1) {
      this.setData({
        isShareEnter: true,
        shareUserId: options.userId
      })
      this.userWxLogin(options.userId)
    }
    // 绑定分享参数
    wx.onCopyUrl(() => {
      return { query: `userId=${app.globalData.userId}&isShare=1` }
    })

  },

  onHide() {
    wx.offCopyUrl()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (!this.data.isShareEnter) {
      this.queryMyShopGoodList();
    }

  },
  userWxLogin(userId) {
    loginWxAction().then((res => {
      console.log("JJLog:登录返回:" + JSON.stringify(res));
      let data = res.data;
      if (res.code == 200) {
        app.globalData.nickName = data.nickname || "";
        app.globalData.avatar = data.avatar || "";
        app.globalData.phone = data.phone || "";
        app.globalData.isLogin = data.isLogin;
        app.globalData.userId = data.uid;
        this.queryShopGoodList(userId);
      }
    })).catch(err => {
      console.error("JJLog:userWxLogin", err);

    })
  },

  //管理商品
  operationGoods() {
    this.setData({
      pageStatus: this.data.pageStatus == 0 ? 1 : 0
    })
  },
  //删除商品
  cancelGoodsAction() {
    let cancelArr = [];
    this.data.goodList.forEach(i => {
      if (i.isSelect) {
        cancelArr.push(i.productId);
      }
    })
    if (cancelArr.length < 1) {
      wx.showToast({
        title: '请先选择要删除的商品',
        icon: "none"
      })
      return;
    }
    if (this.data.goodList.length == cancelArr.length) {
      wx.showToast({
        title: '请至少保留一个商品',
        icon: "none"
      })
      return;
    }
    //调用删除接口：：：
    wx.showModal({
      title: '提示',
      content: '确认删除选中商品?',
      confirmColor: "#ff0000",
      success: res => {
        if (res.confirm) {
          this.deleteMyShopGoods(cancelArr);
        }
      }
    })
  },
  deleteMyShopGoods(cancelArr) {
    wx.showLoading();
    deleteMyShopGoods({ productIds: cancelArr }).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.showToast
          ({
            title: '删除成功...',
            icon: "success"
          })
        this.queryMyShopGoodList();
      }
    }).catch(err => {
      console.error("JJLog:deleteMyShopGoods", err);
      wx.hideLoading()
      wx.showToast
        ({
          title: err || '删除失败...',
          icon: "none"
        })
    })
  },

  //添加商品
  addGoodAction() {
    let num = this.data.goodList.length
    wx.navigateTo({
      url: '/featurePackage/pages/addShop/addShop?num=' + num,
    })
  },
  //
  goodSelectChange(e) {
    let index = e.detail.index;
    let arr = this.data.goodList;
    arr[index].isSelect = !arr[index].isSelect;
    this.setData({
      goodList: arr
    })
  },
  //查询我的
  queryMyShopGoodList() {
    wx.showLoading();
    queryMyShopGoodList().then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        this.setData({
          goodList: res.data.list || []
        })
      }
    }).catch(err => {
      console.error("JJLog:queryMyShopGoodList", err);
      wx.hideLoading()
      wx.showToast
        ({
          title: err || '加载失败...',
          icon: "none"
        })
    })
  },

  queryShopGoodList(userId) {
    wx.showLoading();
    queryShopGoodList(userId).then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        this.setData({
          goodList: res.data.list || []
        })
      }
    }).catch(err => {
      console.error("JJLog:queryShopGoodList", err);
      wx.hideLoading()
      wx.showToast
        ({
          title: err || '加载失败...',
          icon: "none"
        })
    })
  },

  //查询他人的
  queryOtherShopGoodList() {
    wx.showLoading();
    queryOtherShopGoodList().then(res => {
      wx.hideLoading()
      if (res.code == 200) {
        this.setData({
          goodList: res.data.list || []
        })
      }
    }).catch(err => {
      console.error("JJLog:queryOtherShopGoodList", err);
      wx.hideLoading()
      wx.showToast
        ({
          title: err || '加载失败...',
          icon: "none"
        })
    })
  },


  backHomeAction() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let path = "/featurePackage/pages/mySmallShop/mySmallShop?isShare=1" + "&userId=" + app.globalData.userId
    return {
      title: "精选好物，我的小店等你来！",
      path: path
    }
  },
})